package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.config.util.DateTimeUtil;
import com.kws.common.core.domain.R;
import com.kws.common.security.auth.AuthUtil;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.merchant.domain.Merchant;
import com.kws.merchant.domain.MerchantCase;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.po.CheckVo;
import com.kws.merchant.domain.po.MerchantCasePo;
import com.kws.merchant.mapper.MerchantCaseMapper;
import com.kws.merchant.service.MerchantCaseService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.enumDo.RedisConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDate;
import java.util.List;

/**
 * 商家-案例(MerchantCase)表服务实现类
 *
 * @author yxy
 * @since 2024-11-01 09:43:22
 */
@Service("merchantCaseService")
public class MerchantCaseServiceImpl extends ServiceImpl<MerchantCaseMapper, MerchantCase> implements MerchantCaseService {

    @Resource
    private MerchantCaseMapper merchantCaseMapper;
    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    /**
     * 分页查询列表
     */
    @Override
    public Page<MerchantCase> pageList(MerchantCasePo merchantCasePo) {
        Page<MerchantCase> page = new Page<>(merchantCasePo.getPageSize(), merchantCasePo.getTotalPage());
        LambdaQueryWrapper<MerchantCase> queryWrapper = Wrappers.lambdaQuery();
        String caseName = merchantCasePo.getCaseName();
        String status = merchantCasePo.getStatus();
        if (StringUtils.isNotEmpty(caseName)){
            queryWrapper.eq(MerchantCase::getCaseName,caseName);
        }
        if (StringUtils.isNotEmpty(status)){
            queryWrapper.eq(MerchantCase::getStatus,status);
        }
        Page<MerchantCase> page1 = this.page(page, queryWrapper);
        List<MerchantCase> records = page1.getRecords();
        records.forEach(x->{
            x.setFile(FileUtil.fixFileName()+x.getFile());
        });
        return page1;
    }


    /**
     * 商家案例新增
     *
     * @param merchantAwards
     * @return
     */
    @Override
    public Object addMerchantCase(MerchantCase merchantAwards) {
        Long userId = AuthUtil.getLoginUser().getSysUser().getUserId();
        merchantAwards.setMerchantId(userId);
        merchantAwards.setStatus(2);
        merchantAwards.setFile(FileUtil.subFileName(merchantAwards.getFile()));
        return this.save(merchantAwards);
    }

    /**
     * 商家案例审核
     * @param checkVo
     * @return
     */
    @Override
    public Object merchantCaseCheck(CheckVo checkVo) {
        Long id = checkVo.getId();
        MerchantCase merchantCase = this.getById(id);
        if (ObjectUtils.isEmpty(merchantCase)) {
            throw new RuntimeException("没有查到该案例申请");
        }
        Integer status = checkVo.getStatus();
        //审核通过，改变账号状态为正常
        String approvalOpinion = checkVo.getApprovalOpinion();
        //这里的状态和审批状态一致
        merchantCase.setStatus(status);
        merchantCase.setApproval(status);
        merchantCase.setApprovalOpinion(approvalOpinion);
        return this.updateById(merchantCase);
    }

}
